{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Good Triplets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #enumeration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countGoodTriplets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计好三元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>arr</code> ，以及 <code>a</code>、<code>b</code> 、<code>c</code> 三个整数。请你统计其中好三元组的数量。</p>\n",
    "\n",
    "<p>如果三元组 <code>(arr[i], arr[j], arr[k])</code> 满足下列全部条件，则认为它是一个 <strong>好三元组</strong> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= i &lt; j &lt; k &lt;&nbsp;arr.length</code></li>\n",
    "\t<li><code>|arr[i] - arr[j]| &lt;= a</code></li>\n",
    "\t<li><code>|arr[j] - arr[k]| &lt;= b</code></li>\n",
    "\t<li><code>|arr[i] - arr[k]| &lt;= c</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>其中 <code>|x|</code> 表示 <code>x</code> 的绝对值。</p>\n",
    "\n",
    "<p>返回 <strong>好三元组的数量</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>一共有 4 个好三元组：[(3,0,1), (3,0,1), (3,1,1), (0,1,1)] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>arr = [1,1,2,2,3], a = 0, b = 0, c = 1\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>不存在满足所有条件的三元组。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>3 &lt;= arr.length &lt;= 100</code></li>\n",
    "\t<li><code>0 &lt;= arr[i] &lt;= 1000</code></li>\n",
    "\t<li><code>0 &lt;= a, b, c &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-good-triplets](https://leetcode.cn/problems/count-good-triplets/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-good-triplets](https://leetcode.cn/problems/count-good-triplets/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,0,1,1,9,7]\\n7\\n2\\n3', '[1,1,2,2,3]\\n0\\n0\\n1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        res = 0\n",
    "        for i in range(n-2):\n",
    "            for j in range(i+1, n-1):\n",
    "                for k in range(j+1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        cntr = 0\n",
    "        for i, iv in enumerate(arr):\n",
    "            for j, jv in enumerate(arr):\n",
    "                if j <= i:\n",
    "                    continue\n",
    "                elif abs(iv - jv) > a:\n",
    "                    continue\n",
    "                for k, kv in enumerate(arr):\n",
    "                    if k <= j:\n",
    "                        continue\n",
    "                    elif abs(jv - kv) > b:\n",
    "                        continue\n",
    "                    elif abs(iv - kv) > c:\n",
    "                        continue\n",
    "                    else:\n",
    "                        cntr += 1\n",
    "        return cntr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                        ans+=1\n",
    "        return ans            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(arr) - 2):\n",
    "            for j in range(i + 1, len(arr) - 1):\n",
    "                first = abs(arr[i] - arr[j])\n",
    "                if first <= a:\n",
    "                    for k in range(j + 1, len(arr)):\n",
    "                        second = abs(arr[j] - arr[k])\n",
    "                        if second <= b:\n",
    "                            third = abs(arr[i] - arr[k])\n",
    "                            if  third <= c:\n",
    "                                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        temp = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1, len(arr)):\n",
    "                for k in range(j+1, len(arr)):\n",
    "                    if abs(arr[i]-arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c:\n",
    "                        temp += 1\n",
    "        return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(0,len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                if abs(arr[i] - arr[j]) <= a:\n",
    "                    for k in range(j+1,len(arr)):\n",
    "                        if abs(arr[i] - arr[k]) <= c and abs(arr[k] - arr[j]) <= b:\n",
    "                            count = count+1\n",
    "        return count\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        x=0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        x+=1\n",
    "        return x\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(0,len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                if abs(arr[i] - arr[j]) <= a:\n",
    "                    for k in range(j+1,len(arr)):\n",
    "                        if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                            count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        cnt = 0\n",
    "        for i in range(len(arr)-2) :\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                if abs(arr[i]-arr[j])<=a:\n",
    "                    for k in range(j+1,len(arr)):\n",
    "                        if abs(arr[j]-arr[k]) <=b and abs(arr[i]-arr[k])<=c:\n",
    "                            cnt += 1\n",
    "\n",
    "        return cnt\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        cnt=0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[k] - arr[i]) <= c:\n",
    "                        cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        result = 0\n",
    "        for i in range(0,len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                if abs(arr[i]-arr[j]) <= a:\n",
    "                    for k in range(j+1,len(arr)):\n",
    "                        if abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c:\n",
    "                            result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        cnt = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                for k in range(j + 1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        def goold_triple(x,y,z): \n",
    "            return abs(x-y) <= a and abs(y-z) <= b and abs(x-z) <= c \n",
    "        return len([1 for x,y,z in itertools.combinations(arr, 3) if goold_triple(x,y,z)]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        l = len(arr)\n",
    "\n",
    "        for i in range(l):\n",
    "            for j in range(i+1, l):\n",
    "                for k in range(j+1, l):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans, l = 0, len(arr)\n",
    "        for i in range(l - 2):\n",
    "            for j in range(i + 1, l - 1):\n",
    "                for k in range(j + 1, l):\n",
    "                    if (abs(arr[i] - arr[j]) <= a) and (abs(arr[j] - arr[k]) <= b) and (abs(arr[i] - arr[k]) <= c):\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c :\n",
    "                        ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        arr_len = len(arr)\n",
    "        count = 0\n",
    "        for i in range(0, arr_len-2):\n",
    "            for j in range(i+1, arr_len-1):\n",
    "                for k in range(j+1, arr_len):\n",
    "                    if abs(arr[i]-arr[j]) <= a:\n",
    "                        if abs(arr[j]-arr[k]) <= b:\n",
    "                            if abs(arr[i]-arr[k]) <= c:\n",
    "                                count = count + 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n=len(arr)\n",
    "        x=0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                for k in range(j+1,n):\n",
    "                    if -a<=arr[i]-arr[j]<=a and -b<=arr[k]-arr[j]<=b and -c<=arr[i]-arr[k]<=c:\n",
    "                        x+=1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        cnt = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                for k in range(j + 1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        cnt += 1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        # 暴力法\n",
    "        count = 0\n",
    "        nums = len(arr)\n",
    "        for i in range(nums-2):\n",
    "            for j in range(i+1, nums-1):\n",
    "                if arr[i] - arr[j] < -a or arr[i] - arr[j] > a:\n",
    "                    continue\n",
    "                for k in range(j+1, nums):\n",
    "                    if arr[j] - arr[k] < -b or arr[j] - arr[k] > b:\n",
    "                        continue\n",
    "                    if arr[i] - arr[k] < -c or arr[i] - arr[k] > c:\n",
    "                        continue\n",
    "                    count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "      n = 0\n",
    "      for i in range(0, len(arr)-2):\n",
    "        for j in range(i + 1, len(arr)-1):\n",
    "          for k in range(j + 1, len(arr)-0):\n",
    "            if (abs(arr[i]-arr[j]) <= a) & (abs(arr[j]-arr[k]) <= b) & (abs(arr[i]-arr[k]) <= c):\n",
    "              n = n+1\n",
    "      return n\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(arr)-2):\n",
    "            for j in range(i + 1, len(arr)-1):\n",
    "                if abs(arr[i] - arr[j]) > a:\n",
    "                    continue\n",
    "                for k in range(j + 1, len(arr)):\n",
    "                    if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        count += 1\n",
    "\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans=0\n",
    "        n=len(arr)\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                for k in range(j+1,n):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[k]-arr[i])<=c:\n",
    "                        ans+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: \n",
    "        ans = 0\n",
    "        n = len(arr)\n",
    "        for i in range(0, n-2):\n",
    "            for j in range(i+1, n-1):\n",
    "                for k in range(j+1, n):\n",
    "                    if abs(arr[i]-arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c:\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n=len(arr)\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                for k in range(j+1,n):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        n = len(arr)\n",
    "        for i in range(0, n - 2):\n",
    "            for j in range(i + 1, n - 1):\n",
    "                for k in range(j + 1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        ans += 1\n",
    "        return ans;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        nums = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1, len(arr)):\n",
    "                for k in range(j+1, len(arr)):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[k] - arr[j]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        nums += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n=len(arr)\n",
    "        x=0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                for k in range(j+1,n):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                        x+=1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        result = 0\n",
    "\n",
    "        n = len(arr)\n",
    "        for i in range(n-2):\n",
    "            for j in range(i+1,n-1):\n",
    "                for k in range(j+1,n):\n",
    "                    con1 = abs(arr[i] - arr[j])<= a \n",
    "                    con2 = abs(arr[j] - arr[k])<= b\n",
    "                    con3 = abs(arr[i] - arr[k]) <= c\n",
    "                    if con1 and con2 and con3:\n",
    "                        result+=1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        cnt = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                for k in range(j + 1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        cnt += 1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i] - arr[j])<=a and abs(arr[j] - arr[k])<=b and abs(arr[i] - arr[k])<=c:\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n=0\n",
    "        for i in range(0,len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[k] - arr[i]) <= c:\n",
    "                        n+=1\n",
    "        return n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count=0\n",
    "        for i in range(0,len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                        count=count+1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        n = len(arr)\n",
    "        for i in range(0, n - 2):\n",
    "            for j in range(i + 1, n - 1):\n",
    "                if abs(arr[i] - arr[j]) <= a:\n",
    "                    for k in range(j + 1, n):\n",
    "                        if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                            ans += 1\n",
    "        return ans;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        num = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1, len(arr)):\n",
    "                for k in range(j+1, len(arr)):\n",
    "                    if abs(arr[i]-arr[j]) <= a:\n",
    "                        if abs(arr[j]-arr[k]) <= b:\n",
    "                            if abs(arr[i]-arr[k]) <= c:\n",
    "                                num += 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        count = 0\n",
    "        for i in range(n-2):\n",
    "            for j in range(i+1 , n-1):\n",
    "                for k in range(j+1 , n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        if len(arr)<3:\n",
    "            return 0\n",
    "        ans=0\n",
    "        for i in range(len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                          ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        result=0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        result+=1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        n = len(arr)\n",
    "        for i in range(n - 2):\n",
    "            for j in range(i + 1, n - 1):\n",
    "                for k in range(j + 1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        num = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1, len(arr)):\n",
    "                for k in range(j+1, len(arr)):\n",
    "                    if abs(arr[i]-arr[j]) <= a:\n",
    "                        if abs(arr[j]-arr[k]) <= b:\n",
    "                            if abs(arr[i]-arr[k]) <= c:\n",
    "                                num += 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        if len(arr)<3:return 0\n",
    "        count=0\n",
    "        for i in range(0,len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                if abs(arr[i]-arr[j])<=a:\n",
    "                    for k in range(j+1,len(arr)):\n",
    "                        if abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                            count=count+1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        cnt = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                for k in range(j + 1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c: \n",
    "                        cnt += 1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        if len(arr)<3:\n",
    "            return 0\n",
    "        ans=0\n",
    "        for i in range(len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                          ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        i = 0\n",
    "        ans = 0\n",
    "        while i < len(arr) - 2:\n",
    "            j= i + 1\n",
    "            while j < len(arr) - 1:\n",
    "                k = j + 1\n",
    "                while k < len(arr):\n",
    "                    if(abs(arr[i] -arr[j]) <= a and abs(arr[j] -arr[k]) <= b and abs(arr[i] -arr[k]) <= c):\n",
    "                        ans += 1\n",
    "                    k += 1\n",
    "                j += 1\n",
    "            i += 1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        num = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1, len(arr)):\n",
    "                for k in range(j+1, len(arr)):\n",
    "                    if abs(arr[i]-arr[j]) <= a:\n",
    "                        if abs(arr[j]-arr[k]) <= b:\n",
    "                            if abs(arr[i]-arr[k]) <= c:\n",
    "                                num += 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        count = 0\n",
    "        for i in range(n-2) :\n",
    "            i_min = arr[i] - a \n",
    "            i_max = arr[i] + a \n",
    "            for j in range(i+1, n-1) :\n",
    "                if arr[j] >= i_min and arr[j] <= i_max :\n",
    "                    k_max = min(arr[i]+c, arr[j]+b)\n",
    "                    k_min = max(arr[i]-c, arr[j]-b)\n",
    "                    for k in range(j+1, n) :\n",
    "                        if arr[k] >= k_min and arr[k] <= k_max :\n",
    "                            count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        m=0\n",
    "        for i in range(len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                for k in range(j+1,len(arr)-0):\n",
    "                    if((abs(arr[i]-arr[j]) <= a)and(abs(arr[j]-arr[k]) <=b)and(abs(arr[i]-arr[k]) <=c)):\n",
    "                        m+=1\n",
    "        return m\n",
    "\n",
    "                    \n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1, n):\n",
    "                if abs(arr[i]-arr[j])>a:\n",
    "                    continue\n",
    "                else:\n",
    "                    for k in range(j+1, n):\n",
    "                        if abs(arr[j]-arr[k])>b or abs(arr[i]-arr[k])>c:\n",
    "                            continue\n",
    "                        else:\n",
    "                            res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        num=0\n",
    "        for i in range(len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                if abs(arr[i]-arr[j])<=a:\n",
    "                    for k in range(j+1,len(arr)):\n",
    "                        if abs(arr[i]-arr[k])<=c and abs(arr[j]-arr[k])<=b:\n",
    "                            num+=1\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: list[int], a: int, b: int, c: int) -> int:\n",
    "        sum=0\n",
    "        for i in range(0,len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                        sum+=1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(len(arr)):\n",
    "                for k in range(len(arr)):\n",
    "                    if -a <= arr[i]-arr[j] <= a and -b <= arr[j]-arr[k] <= b and -c <= arr[i]-arr[k] <= c and 0<= i <j < k < len(arr):\n",
    "                        ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        l = len(arr)\n",
    "        num = 0\n",
    "        for i in range(0,l-2):\n",
    "            for j in range(i+1,l-1):\n",
    "                for k in range(j+1,l):\n",
    "                    if (abs(arr[i] - arr[j]) > a)or(abs(arr[j] - arr[k]) > b)or(abs(arr[i] - arr[k]) > c):\n",
    "                        continue\n",
    "                    else:\n",
    "                        num += 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(len(arr)):\n",
    "                for k in range(len(arr)):\n",
    "                    if i < j < k and abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        n += 1\n",
    "\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        l=len(arr)\n",
    "        sum=0\n",
    "        for i in range(l-2):\n",
    "            for j in range(i+1,l-1):\n",
    "                for k in range(j+1,l):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        sum+=1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        sum=0\n",
    "        for i in range(len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j]) <=a and abs(arr[j]-arr[k]) <=b and abs(arr[k]-arr[i]) <= c:\n",
    "                        sum +=1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        n = len(arr)\n",
    "        ncount = 0\n",
    "        for i in range(0,n-2):\n",
    "            for j in range(1,n-1):\n",
    "                if(j<=i):\n",
    "                    continue\n",
    "                for k in range(2,n):\n",
    "                    if(k<=j):\n",
    "                        continue\n",
    "                    if(abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c):\n",
    "                        ncount += 1\n",
    "        return ncount\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        length = len(arr)\n",
    "        count = 0\n",
    "        for i in range(length):\n",
    "            for j in range(i + 1, length):\n",
    "                for k in range(j + 1, length):\n",
    "                    if 0 <= i < j < k < length and abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        res = 0\n",
    "        n = len(arr)\n",
    "        for i in range(n-2):\n",
    "            for j in range(i+1,n-1):\n",
    "                for k in range(j+1,n):\n",
    "                    if(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c):\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count=0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(len(arr)):\n",
    "                for k in range(len(arr)):\n",
    "                    if i<j<k and abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                        count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if 0 <= i < j < k < len(arr) and abs(arr[i]-arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c:\n",
    "                        count +=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "       \n",
    "        count = 0\n",
    "        for i in range(0,len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i]-arr[k])<= c:\n",
    "                 \n",
    "                        count += 1\n",
    "                      \n",
    "\n",
    "        return count\n",
    "\n",
    "        # n=0\n",
    "        # for i in range(0,len(arr)):\n",
    "        #     for j in range(i+1,len(arr)):\n",
    "        #         for k in range(j+1,len(arr)):\n",
    "        #             if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[k] - arr[i]) <= c:\n",
    "        #                 n+=1\n",
    "        # return n\n",
    "\n",
    "\n",
    "\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        res=0\n",
    "        n=len(arr)\n",
    "        for i in range(n-2):\n",
    "            for j in range(i+1,n-1):\n",
    "                if abs(arr[i]-arr[j])>a:\n",
    "                    continue\n",
    "                for z in range(j+1,n):\n",
    "                    if abs(arr[j]-arr[z])<=b and abs(arr[i]-arr[z])<=c:\n",
    "                        res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        n = len(arr)\n",
    "        total = [0] * 1001\n",
    "        for j in range(n):\n",
    "            for k in range(j + 1, n):\n",
    "                if abs(arr[j] - arr[k]) <= b:\n",
    "                    lj, rj = arr[j] - a, arr[j] + a\n",
    "                    lk, rk = arr[k] - c, arr[k] + c\n",
    "                    l = max(0, lj, lk)\n",
    "                    r = min(1000, rj, rk)\n",
    "                    if l <= r:\n",
    "                        ans += total[r] if l == 0 else total[r] - total[l - 1]\n",
    "            for k in range(arr[j], 1001):\n",
    "                total[k] += 1\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        ret = 0\n",
    "        for k in range(len(arr)):\n",
    "            for j in range(k):\n",
    "                for i in range(j):\n",
    "                    if (abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c):\n",
    "                        ret += 1;\n",
    "        return ret;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    绝对值1=abs(arr[i]-arr[j])\n",
    "                    绝对值2=abs(arr[j]-arr[k])\n",
    "                    绝对值3=abs(arr[i]-arr[k])\n",
    "                    if 0<=i<j<k<len(arr) and 绝对值1<=a and 绝对值2<=b and 绝对值3<=c:\n",
    "                        count+=1\n",
    "        print(f\"{count}\")\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c :\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        result = 0\n",
    "        \n",
    "        for i in range(0, len(arr)):\n",
    "            for j in range(i + 1, len(arr)):\n",
    "                for k in range(j + 1, len(arr)):\n",
    "                    if (i < j) and (j < k):\n",
    "                        if (abs(arr[i] - arr[j]) <= a) and (abs(arr[j] - arr[k]) <= b) and (abs(arr[i] - arr[k]) <= c):\n",
    "                            result += 1\n",
    "\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        x=len(arr)\n",
    "        count=0\n",
    "        for i in range(x):\n",
    "            for j in range(i+1,x):\n",
    "                for k in range(j+1,x):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:\n",
    "                        count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        res=0\n",
    "        n=len(arr)\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                for z in range(j+1,n):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[z])<=b and abs(arr[i]-arr[z])<=c:\n",
    "                        res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        n = len(arr)\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                for k in range(j + 1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        n = len(arr)\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                for k in range(j + 1, n):\n",
    "                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1,len(arr)):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c:\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:\n",
    "        cnt=0\n",
    "        for i in range(len(arr)-2):\n",
    "            for j in range(i+1,len(arr)-1):\n",
    "                for k in range(j+1,len(arr)):\n",
    "                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[k]-arr[i])<=c:\n",
    "                        cnt+=1\n",
    "        return cnt"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
